<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
        :where="defaultWhere"
      >
        <template #buy="{ row }">
          <span class="pointer blue" @click="openOffer(row)">查看</span>
        </template>
        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="danger" size="small" @click="openReoffer(row)">
              重新报价
            </el-button>
            <el-button type="success" size="small" @click="read(row)">
              已阅
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>

    <!-- 重新报价 -->
    <re-offer
      v-model="showReoffer"
      action="reoffer"
      :data="current"
      @done="reload"
    />

    <!-- 报价列表 -->
    <offer-list v-model="showOffer" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import { pageInsurancePlanList, setInsurancePlanStatus } from '@/api/company';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';
  import Search from './components/search.vue';
  import OfferList from './components/offer-list.vue';
  import ReOffer from './components/re-offer.vue';

  /** 表格实例 */
  const tableRef = ref(null);
  const defaultWhere = {
    status: 10
  };

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: 'ID'
      },
      {
        width: 100,
        align: 'center',
        prop: 'offer_user',
        label: '报价人'
      },
      {
        width: 140,
        align: 'center',
        prop: 'offer_time',
        label: '报价时间'
      },
      {
        width: 90,
        slot: 'buy',
        label: '报价详情',
        align: 'center'
      },
      {
        prop: 'remark',
        label: '报价要求'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '车牌'
      },
      {
        width: 180,
        prop: 'company',
        label: '车队'
      },
      {
        width: 100,
        align: 'center',
        prop: 'create_user',
        label: '申请人'
      },
      {
        width: 200,
        slot: 'action',
        label: '操作',
        align: 'center'
      }
    );

    return cols;
  });

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsurancePlanList({
      ...where,
      ...orders,
      page,
      limit,
      action: 'choose'
    });
    return result;
  };
  /** 搜索 */
  const reload = (where) => {
    tableRef.value?.reload?.({ page: 1, where });
  };

  const current = ref([]);

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  /** 查看出单要求 */
  const showOffer = ref(false);
  const openOffer = (row) => {
    current.value = row;
    showOffer.value = true;
  };

  /** 重新报价 */
  const showReoffer = ref(false);
  const openReoffer = (row) => {
    current.value = row;
    showReoffer.value = true;
  };

  /** 已阅 */
  const read = (row = null) => {
    ElMessageBox.confirm(
      '确定要将“' + row.plate_number + '”的报价设为已阅吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');

        setInsurancePlanStatus({ id: row.id, status: 20 })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>
